﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using agile_tool.userControl;

namespace agile_tool
{
    /// <summary>
    /// Interaction logic for resourcePage.xaml
    /// </summary>
    public partial class resourcePage : UserControl, IGenericPagesData
    {
        Button createNewResources;
        create createresourceItem;
        int userId;
        string filter = "";
        public string navigationName = "";

        
         public resourcePage(int userId, string filter)
         {
             InitializeComponent();
             this.userId = userId;
             this.filter = filter;
             navigationName = "Resources";
             refresh("");
         }
        
          public string getNavigationName()
         {
             return navigationName;
         }
         public int getId()
         {
             return 0;
         }
         public void refresh(string filter)
         {
             lbresources.Items.Clear();
            
             try
             {

                 DataClasses1DataContext db = new DataClasses1DataContext();
                 var resources = from p in db.tblResources  select p;


                 foreach (var p in resources)
                 {
                     if (filter == "")
                     {
                         resourceItem theResourcesItem = new resourceItem();
                         theResourcesItem.tbName.Text = p.name;
                         theResourcesItem.tbDesc.Text = p.description;
                         theResourcesItem.id = p.id;
                         theResourcesItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                         theResourcesItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);

                         lbresources.Items.Add(theResourcesItem);
                     }
                     else
                     {
                         if (p.name.IndexOf(filter) != -1)
                         {
                             resourceItem theResourcesItem = new resourceItem();
                             theResourcesItem.tbName.Text = p.name;
                             theResourcesItem.tbDesc.Text = p.description;
                             theResourcesItem.id = p.id;
                             theResourcesItem.btnDell.Click += new RoutedEventHandler(btnDell_Click);
                             theResourcesItem.btnEdit.Click += new RoutedEventHandler(btnEdit_Click);

                             lbresources.Items.Add(theResourcesItem);
                         }
                     }

                 }
             }
             catch { }
             createNewResources = new Button { Content = "Create New Resources" };
             createNewResources.Click += new RoutedEventHandler(createNewResources_Click);
             createNewResources.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
             lbresources.Items.Add(createNewResources);
         }

    
       
         void btnEdit_Click(object sender, RoutedEventArgs e)
         {
             resourceItem theResourcesItem = new resourceItem();
             theResourcesItem = (resourceItem)helper.FindParrentUserControl((Button)sender, theResourcesItem.GetType().ToString());
             if (theResourcesItem.spSlide.Height == 0)
             {
                 theResourcesItem.openTheDesc();
                 theResourcesItem.btnEdit.Content = "Save";
             }
             else
             {
                 if (theResourcesItem.tbName.Text == "")
                 { MessageBox.Show("The Name Can't be Empty"); }
                 else
                 {
                     theResourcesItem.closeTheDesc();
                     theResourcesItem.btnEdit.Content = "Edit";
                     DataClasses1DataContext db = new DataClasses1DataContext();
                     tblResource tb = (from p in db.tblResources where p.id == theResourcesItem.id select p).First();
                     tb.name = theResourcesItem.tbName.Text;
                     tb.description = theResourcesItem.tbDesc.Text;
                     db.SubmitChanges();
                 }
             }
         }

         void btnDell_Click(object sender, RoutedEventArgs e)
         {
             try
             {
                 resourceItem theResourcesItem = new resourceItem();
                 theResourcesItem = (resourceItem)helper.FindParrentUserControl(((Button)sender), theResourcesItem.GetType().ToString());
                 DataClasses1DataContext db = new DataClasses1DataContext();
                 tblResource tb = (from p in db.tblResources where p.id == theResourcesItem.id select p).First();
                 db.tblResources.DeleteOnSubmit(tb);
                 db.SubmitChanges();

                 lbresources.Items.Remove(theResourcesItem);
             }
             catch { MessageBox.Show("You can't delete assigned resources"); }
         }

         void createNewResources_Click(object sender, RoutedEventArgs e)
         {
             lbresources.Items.Remove(createNewResources);
             createresourceItem = new create();
             createresourceItem.btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
             createresourceItem.btnSave.Click += new RoutedEventHandler(btnSave_Click);

             lbresources.Items.Add(createresourceItem);
         }

         void btnSave_Click(object sender, RoutedEventArgs e)
         {
             if (createresourceItem.tbName.Text != "")
             {
                 DataClasses1DataContext db = new DataClasses1DataContext();
                 tblResource tp = new tblResource { name = createresourceItem.tbName.Text, description = createresourceItem.tbDesc.Text  };
                 
                 db.tblResources.InsertOnSubmit(tp);
                 db.SubmitChanges();

                 refresh("");
             }
             else
             {
                 MessageBox.Show("You need a name for your resource");
             }
         }

         void btnCancel_Click(object sender, RoutedEventArgs e)
         {
             refresh("");
              
         }
    }
}
